<?php


namespace app\common\business;


use app\common\model\QuestionDetail;
use catchAdmin\registration\service\TaskService;
use catcher\exceptions\FailedException;
use catcher\exceptions\MessageException;
use think\Exception;

class Question
{
    protected $model = null;

    public function __construct()
    {

    }

    public function getList()
    {


    }

    public function createQuestion($patientInfo, $projectInfo, $creatorInfo = [])
    {

        try {
            # 拼接推送数组
            $data = [
                'projectId' => $projectInfo["projectId"],
                'creator_id' => $creatorInfo["creator_id"] ?? request()->user()->id,
                'putChanel' => 'ALL',
                'deleted_at' => 0,
                'patientList' => [
                    [
                        'sex' => $patientInfo['sex'] ?? "",
                        'age' => $patientInfo['inage'] ?? 0,
                        'name' => $patientInfo['name'] ?? "",
                        'mobile' => $patientInfo['presentTelphone'] ?? "",
                        'patient_id' => $patientInfo["patientId"],
                        'creator_id' => $creatorInfo["creator_id"] ?? request()->user(),
                        'creator_name' => $creatorInfo["username"] ?? request()->user()->username,
                        'creator_dept_id' => $creatorInfo['creator_dept_id'] ?? request()->user()->userDept->id,
                        'creator_dept_name' => $creatorInfo['creator_dept_name'] ?? (request()->user())->userDept->department_name,
                        'patientDepId' => $patientInfo['timeLine'][0]['deptId'] ?? "",
                        'patientdeptName' => $patientInfo['timeLine'][0]['deptName'] ?? "",
                    ]
                ]
            ];

            $result = (new Answer())->initQuestion($data);
            if (isset($result["answer_ids"]) && !empty($result["answer_ids"]) && empty($creatorInfo)) {
                try {
                    (new Message())->saveMessage(3, $projectInfo["projectId"], $projectInfo["name"], $patientInfo["patientId"], $patientInfo["name"]);
                } catch (\Exception $exception) {
                    throw new MessageException("推送消息失败：失败原因:" . $exception->getMessage() . $exception->getFile() . $exception->getLine());
                }
            }
            return $result["answer_ids"] ?? "";
        } catch (Exception $exception) {
            throw new FailedException($exception->getMessage());
        }
    }
}